//
//  HSQGoodsCollectionViewController.m
//  LiTianDecoration
//
//  Created by administrator on 2018/4/29.
//  Copyright © 2018年 administrator. All rights reserved.
//

#define KTitlesViewHeight 50

#define KalbumViewH KTitlesViewHeight  + KSafeTopeHeight

#import "HSQGoodsCollectionViewController.h"
#import "HSQGoodsCollectionListCell.h"
#import "HSQMyCollectionHeaderView.h"
#import "HSQAccountTool.h"
#import "HSQCategoryListModel.h" // 分类的模型
#import "HSQGoodsDataListModel.h" // 收藏商品的模型
#import "HSQGoodsModelView.h"
#import "HSQShopCarManger.h"
#import "HSQShopCarGoodsListModel.h"

#import "HSQSortDropMenuView.h"
#import "MVGoodsDetailHomeViewController.h"

@interface HSQGoodsCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,HSQMyCollectionHeaderViewDelegate,HSQGoodsModelViewDelegate,HSQSortDropMenuViewDelegate>

@property (nonatomic, weak) UIView *titlesView; // 顶部的所有标签

@property (nonatomic, weak) UIButton *selectedButton; // 当前选中的按钮

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, weak) UIButton *ClassButton; // 分类的按钮
@property (nonatomic, strong) UILabel *ClassName_Label;  // 分类的名字
@property (nonatomic, strong) UIImageView *Class_image;  // 分类的图片

@property (nonatomic, strong) UIView *albumsBgView; // 下拉菜单的黑色的背景图

@property (nonatomic, strong) HSQSortDropMenuView *albumView; // 下拉菜单的黑色的背景图

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSource; // 商品的数组

@property (nonatomic, strong) NSMutableArray *categoryList; // 分类的数组

@property (nonatomic, assign) NSInteger currentPage; // 当前的页数

@property (nonatomic, strong) HSQNoDataView *noDataView;

@property (nonatomic, copy) NSString *totalPage;  // 总的页数

@property (nonatomic, copy) NSString *commonId;  // 商品的sku

@property (nonatomic, assign) NSInteger IsEditState;  // 是否处于编辑状态

@property (nonatomic, strong) UIView *Dete_View; // 底部的删除视图

@property (nonatomic, strong) UILabel *Select_Label; // 底部的选中个数

@property (nonatomic, strong) UIButton *Dete_Button; // 底部的删除按钮

@property (nonatomic, strong) NSMutableArray *Select_Array; // 选中删除的商品数组

@property (nonatomic, strong) NSMutableArray *AllGoods_Array; // 全部收藏的数据

@property (nonatomic, strong) NSMutableArray *default_source; // 默认筛选结果

@property (nonatomic, strong) NSMutableArray *price_source; // 降价筛选结果

@property (nonatomic, strong) NSMutableArray *Sales_source; // 促销筛选结果

@property (nonatomic, strong) NSMutableArray *Class_source; //分类筛选结果

@property (nonatomic, copy) NSString *Current_String;  // 当前的排序条件 0 代表默认 1代表降价 2 促销

@property (nonatomic, copy) NSString *categoryId; // 当前选中的分类

@property (nonatomic, copy) NSString *categorySort;  // 排序



@end

@implementation HSQGoodsCollectionViewController

-(NSMutableArray *)Select_Array{
    
    if (_Select_Array == nil) {
        
        self.Select_Array = [NSMutableArray array];
    }
    
    return _Select_Array;
}

-(NSMutableArray *)AllGoods_Array{
    
    if (_AllGoods_Array == nil) {
        
        self.AllGoods_Array = [NSMutableArray array];
    }
    
    return _AllGoods_Array;
}

-(NSMutableArray *)Class_source{
    
    if (_Class_source == nil) {
        
        self.Class_source = [NSMutableArray array];
    }
    
    return _Class_source;
}

-(NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}

-(NSMutableArray *)categoryList{
    
    if (_categoryList == nil) {
        
        self.categoryList = [NSMutableArray array];
    }
    
    return _categoryList;
}

-(NSMutableArray *)price_source{
    
    if (_price_source == nil) {
        
        self.price_source = [NSMutableArray array];
    }
    
    return _price_source;
}

-(NSMutableArray *)Sales_source{
    
    if (_Sales_source == nil) {
        
        self.Sales_source = [NSMutableArray array];
    }
    
    return _Sales_source;
}

-(NSMutableArray *)default_source{
    
    if (_default_source == nil) {
        
        self.default_source = [NSMutableArray array];
    }
    
    return _default_source;
}

- (HSQNoDataView *)noDataView{
    
    if (!_noDataView) {
        
        _noDataView = [[HSQNoDataView alloc] initWithTitle:@"亲，还没有收藏的商品额" imageName:@"WaitingForView" height:50 TopMargin:0];
    }
    return _noDataView;
}

/**
 * @brief 分类的黑色背景
 */
- (UIView *)albumsBgView{
    
    if (!_albumsBgView) {
        
        _albumsBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KTitlesViewHeight, self.view.frame.size.width, self.view.frame.size.height - KTitlesViewHeight)];
        
        _albumsBgView.hidden = YES;
        
        _albumsBgView.userInteractionEnabled = YES;
        
        _albumsBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        
        [_albumsBgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didAlbumsBgViewClick)]];
    }
    return _albumsBgView;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的收藏";
    
    self.view.backgroundColor = KViewBackGroupColor;
    
    self.Current_String = @"0";
    
    self.categoryId = @"-100";
    
    // 0.创建CollectionView
    [self SetUpCollectionView];
    
    // 1.设置顶部标题栏
    [self setupTopTitlesView];
    
    // 请求商品收藏的数据
    [self RequestDataFromTheMerchandiseCollection];
    
    // 底部删除视图
    [self addBottomDeleteView];
    
    // 监听是否编辑的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(EditNotif:) name:@"SendIsEditStateNotif" object:nil];
    
}

/**
 * @brief 设置顶部标题栏
 */
- (void)setupTopTitlesView{
    
    [self.view addSubview:self.albumsBgView];
    
    // 创建分类下拉视图
    HSQSortDropMenuView *albumView = [[HSQSortDropMenuView alloc] initWithFrame:CGRectMake(0, -(KalbumViewH + 340), KScreenWidth, 340)];
    albumView.delegate = self;
    [self.view addSubview:albumView];
    self.albumView = albumView;
    
    NSArray *titlesArray = @[@"默认",@"降价",@"促销"];
    
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor];
    titlesView.mj_w = [UIScreen mainScreen].bounds.size.width * 0.75;
    titlesView.mj_h = KTitlesViewHeight;
    titlesView.mj_y = 0;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 内部的子标签
    CGFloat width = titlesView.mj_w / titlesArray.count;
    
    CGFloat height = titlesView.mj_h;
    
    for (NSInteger i = 0; i < titlesArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        button.tag = i;
        
        button.mj_h = height;
        
        button.mj_w = width;
        
        button.mj_x = i * width;
        
        [button setTitle:titlesArray[i] forState:UIControlStateNormal];
        
        [button setTitleColor:RGB(131, 131, 131) forState:UIControlStateNormal];
        
        [button setTitleColor:RGB(255, 83, 63) forState:UIControlStateDisabled];
        
        button.titleLabel.font = [UIFont systemFontOfSize:13.0];
        
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0)
        {
            button.enabled = NO;
            
            self.selectedButton = button;
            
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
        }
    }
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(KScreenWidth * 0.75, 0, KScreenWidth / 4, KTitlesViewHeight)];
    bgView.backgroundColor = titlesView.backgroundColor;
    [self.view addSubview:bgView];
    self.bgView = bgView;
    
    CGSize class_size = [NSString SizeOfTheText:@"分类" font:[UIFont systemFontOfSize:13.0] MaxSize:CGSizeMake(KScreenWidth / 4 - 15 - 8, KTitlesViewHeight)];
    UILabel *className_Label = [[UILabel alloc] init];
    className_Label.text = @"分类";
    className_Label.textColor = RGB(131, 131, 131);
    className_Label.font = [UIFont systemFontOfSize:13.0];
    className_Label.textAlignment = NSTextAlignmentRight;
    className_Label.frame = CGRectMake((self.bgView.mj_w - class_size.width - 5 - 8)/2, 0, class_size.width, KTitlesViewHeight);
    [bgView addSubview:className_Label];
    self.ClassName_Label = className_Label;
    
    UIImageView *class_image = [[UIImageView alloc] initWithImage:KImageName(@"F29714FE-BAB1-48BE-AED0-4825E2E9BFB6")];
    class_image.frame = CGRectMake(CGRectGetMaxX(className_Label.frame)+5, (KTitlesViewHeight - 6)/2, 8, 6);
    [bgView addSubview:class_image];
    self.Class_image = class_image;
    
    // 创建分类按钮
    UIButton *class_btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    class_btn.backgroundColor = [UIColor clearColor];
    [class_btn addTarget:self action:@selector(class_btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    class_btn.frame = CGRectMake(0, 0, KScreenWidth / 4, KTitlesViewHeight);
    [bgView addSubview:class_btn];
    self.ClassButton = class_btn;
    

    
    
}

/**
 * @brief 顶部按钮的点击事件
 */
- (void)titleClick:(UIButton *)button{
    
    [self.dataSource removeAllObjects];
    
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    
    button.enabled = NO;
    
    self.selectedButton = button;
    
    self.Current_String = [NSString stringWithFormat:@"%ld",(long)button.tag];
    
    if (button.tag == 0) // 点击了默认
    {
        [self.dataSource addObjectsFromArray:self.Class_source];
    }
    else  if (button.tag == 1) // 点击了降价
    {
        NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"goodsCommon.appPrice0" ascending:YES];
        
        NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
        
        NSMutableArray *paixu_array = [NSMutableArray arrayWithArray:self.Class_source];
        
        paixu_array = [[paixu_array sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];
        
        [self.dataSource addObjectsFromArray:paixu_array];

    }
    else  if (button.tag == 2) // 点击了促销
    {
        for (NSInteger i = 0; i < self.Class_source.count; i ++) {
            
            HSQGoodsDataListModel *model_02 = self.Class_source[i];
            
            if ([model_02.goodsCommon[@"joinBigSale"] integerValue] == 1)
            {
                [self.dataSource addObject:model_02];
            }
        }
    }
    
    [self.collectionView reloadData];
    
}


/**
 * @brief 分类按钮的点击
 */
- (void)class_btnClickAction:(UIButton *)sender{
    
    self.ClassButton.enabled = NO;
    
    [self ShowTheCategoryDropdownList];
}

/**
 * @brief 显示分类下拉列表
 */
- (void)ShowTheCategoryDropdownList{
    
    self.albumsBgView.hidden = NO;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.albumView.frame = CGRectMake(0, KTitlesViewHeight, self.view.frame.size.width, 340);
        
        self.albumView.categoryList = self.categoryList;
        
        self.albumView.categorySort = self.categorySort;
        
        self.albumsBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        
        self.Class_image.transform = CGAffineTransformMakeRotation(M_PI);
    }];
}

/**
 * @brief 黑色背景视图的点击
 */
- (void)didAlbumsBgViewClick{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.albumView.frame = CGRectMake(0, -(KalbumViewH + 340),self.view.frame.size.width, 340);
        
        self.albumsBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
        self.Class_image.transform = CGAffineTransformMakeRotation(M_PI * 2);
        
    } completion:^(BOOL finished) {
        
        self.albumsBgView.hidden = YES;
        
        self.ClassButton.enabled = YES;
    }];
}

/**
 * @brief 分类下拉菜单视图按钮的点击
 */
- (void)CategoryMenuSelectedClickEvents:(UITableView *)tableView model:(HSQCategoryListModel *)model{
    
    [self.dataSource removeAllObjects];
    [self.default_source removeAllObjects];
    [self.price_source removeAllObjects];
    [self.Sales_source removeAllObjects];
    [self.Class_source removeAllObjects];
    
    self.ClassButton.enabled = YES;
    self.categorySort = model.categorySort;
    
    CGSize class_size = [NSString SizeOfTheText:model.categoryName font:[UIFont systemFontOfSize:13.0] MaxSize:CGSizeMake(KScreenWidth / 4 - 15 - 8, KTitlesViewHeight)];
    if ([model.categoryName isEqualToString:@"全部"])
    {
        self.ClassName_Label.text = @"分类";
        self.ClassName_Label.textColor = RGB(131, 131, 131);
    }
    else
    {
        self.ClassName_Label.text = model.categoryName;
        self.ClassName_Label.textColor = RGB(255, 83, 63);
    }
    
    self.ClassName_Label.frame = CGRectMake((self.bgView.mj_w - class_size.width - 5 - 8)/2, 0, class_size.width, KTitlesViewHeight);
    
    self.Class_image.frame = CGRectMake(CGRectGetMaxX(self.ClassName_Label.frame)+5, (KTitlesViewHeight - 6)/2, 8, 6);
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.albumView.frame = CGRectMake(0, -(KalbumViewH + 340),self.view.frame.size.width, 340);
        
        self.albumsBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
        self.Class_image.transform = CGAffineTransformMakeRotation(M_PI * 2);
        
    } completion:^(BOOL finished) {
        
        self.albumsBgView.hidden = YES;
        
    }];
    
    self.categoryId = model.categoryId;
    
    if (self.categoryId.integerValue == -100) // 分类
    {
        [self.Class_source addObjectsFromArray:self.AllGoods_Array];
    }
    else
    {
        for (HSQGoodsDataListModel *model_01 in self.AllGoods_Array) {
            
            NSString *categoryId1 = [NSString stringWithFormat:@"%@",model_01.goodsCommon[@"categoryId1"]];
            
            if ([categoryId1 isEqualToString:self.categoryId]) {
                
                [self.Class_source addObject:model_01];
            }
        }
    }
    
    if (self.Current_String.integerValue == 0) // 默认筛选
    {
        [self.default_source addObjectsFromArray:self.Class_source];
        
        [self.dataSource addObjectsFromArray:self.default_source];
    }
    else if (self.Current_String.integerValue == 1) // 降价筛选
    {
        NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"goodsCommon.appPrice0" ascending:YES];
        
        NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
        
        NSMutableArray *paixu_array = [NSMutableArray arrayWithArray:self.Class_source];
        
        paixu_array = [[paixu_array sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];
        
        [self.dataSource addObjectsFromArray:paixu_array];
    }
    else if (self.Current_String.integerValue == 2) // 促销
    {
        for (NSInteger i = 0; i < self.Class_source.count; i ++) {
            
            HSQGoodsDataListModel *model_02 = self.Class_source[i];
            
            if ([model_02.goodsCommon[@"joinBigSale"] integerValue] == 1)
            {
                [self.Sales_source addObject:model_02];
            }
        }
        
        [self.dataSource addObjectsFromArray:self.Sales_source];
    }
    
    [self.collectionView reloadData];
}


/**
 * @brief 创建CollectionView
 */
- (void)SetUpCollectionView{
    
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc] init];
    
    Layout.minimumLineSpacing = 5;  // 最小的行间距
    
    Layout.minimumInteritemSpacing = 5; // 最小的列间距
    
    CGFloat collectionHeight = KScreenHeight - KSafeBottomHeight  - KSafeTopeHeight  - KTitlesViewHeight;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, KTitlesViewHeight, KScreenWidth, collectionHeight) collectionViewLayout:Layout];
    
    collectionView.backgroundColor = [UIColor clearColor];
    
    collectionView.dataSource = self;
    
    collectionView.delegate = self;
    
    [collectionView registerClass:[HSQGoodsCollectionListCell class] forCellWithReuseIdentifier:@"HSQGoodsCollectionListCell"];
    
    [collectionView registerClass:[HSQMyCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HSQMyCollectionHeaderView"];
    
    [self.view addSubview:collectionView];
    
    [self.view sendSubviewToBack:collectionView];
    
    self.collectionView = collectionView;
    
}

/**
 * @brief 请求商品收藏的数据
 */
- (void)RequestDataFromTheMerchandiseCollection{
    
    // 1.下拉加载更多的数据
    self.collectionView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(LoadNewGoodsCollectionListData)];
    
    [self.collectionView.mj_header beginRefreshing];
    
    // 3.上啦加载更多的代码
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMoreGoodsCollectionListData)];
}

/**
 * @brief 加载最新的收藏的商品数据
 */
- (void)LoadNewGoodsCollectionListData{
    
    [[HSQProgressHUDManger Manger] ShowLoadingDataFromeServer:@"" ToView:self.view IsClearColor:YES];
    
    // 0.清空数组
    [self.AllGoods_Array removeAllObjects];
    [self.dataSource removeAllObjects];
    [self.default_source removeAllObjects];
    [self.price_source removeAllObjects];
    [self.Sales_source removeAllObjects];
    [self.Class_source removeAllObjects];
    
    // 结束上啦
    [self.collectionView.mj_footer endRefreshing];
    
    self.currentPage = 1;
    
    NSDictionary *params = @{@"token":[HSQAccountTool account].token,@"page": @(self.currentPage)};
    
    AFNetworkRequestTool *requestTool = [AFNetworkRequestTool shareRequestTool];
    
    [requestTool.manger POST:UrlAdress(KGoodsCollectionListUrl) parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [[HSQProgressHUDManger Manger] DismissProgressHUD];
        
        HSQLog(@"===商品收藏数据===%@",responseObject);
        
        self.totalPage = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"pageEntity"][@"totalPage"]];
        
        if ([responseObject[@"code"] integerValue] == 200)
        {
            // 分类的数据
            self.categoryList = [HSQCategoryListModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"categoryList"]];
            HSQCategoryListModel *model = [[HSQCategoryListModel alloc] init];
            model.categoryName = @"全部";
            model.categorySort = @"0";
            model.categoryId = @"-100";
            [self.categoryList insertObject:model atIndex:0];
            
            // 收藏商品的数组
            NSArray *favGoodsList = responseObject[@"datas"][@"favGoodsList"];
            if (favGoodsList.count == 0)
            {
                [self.collectionView addSubview:self.noDataView];
            }
            else
            {
                for (NSDictionary *diction in favGoodsList) {
                    
                    HSQGoodsDataListModel *model = [[HSQGoodsDataListModel alloc] init];
                    model.IsOpen = @"0";
                    model.IsEditState = @"1";
                    model.IsSelectState = @"1";
                    [model setValuesForKeysWithDictionary:diction];
                    [self.AllGoods_Array addObject:model];
                }

                // 筛选数据到制定的位置
                [self FilterDataToEstablishedLocations];

            }
        }
        else
        {
            NSString *errorString = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"error"]];
            
            [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:errorString SuperView:self.view];
        }
        
        [self.collectionView.mj_header endRefreshing];
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 结束刷新
        [self.collectionView.mj_header endRefreshing];
        
        [self.collectionView addSubview:self.noDataView];
        
        // 提示数据请求失败
        [[HSQProgressHUDManger Manger] ShowProgressHUDPromptText:KErrorPlacherString SupView:self.view];
    }];
}

/**
 * @brief 加载更多的收藏的商品数据
 */
- (void)LoadMoreGoodsCollectionListData{
    
    [self.collectionView.mj_header endRefreshing];
    
     NSDictionary *params = @{@"token":[HSQAccountTool account].token,@"page": @(++self.currentPage)};
    
    AFNetworkRequestTool *requestTool = [AFNetworkRequestTool shareRequestTool];
    
    [requestTool.manger POST:UrlAdress(KGoodsCollectionListUrl) parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [[HSQProgressHUDManger Manger] DismissProgressHUD];
        
        HSQLog(@"===加载更多收藏数据===%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 200)
        {
            NSArray *favGoodsList = responseObject[@"datas"][@"favGoodsList"];
            
            for (NSDictionary *diction in favGoodsList) {
                
                HSQGoodsDataListModel *model = [[HSQGoodsDataListModel alloc] init];
                model.IsOpen = @"0";
                model.IsEditState = @"1";
                model.IsSelectState = @"1";
                [model setValuesForKeysWithDictionary:diction];
                [self.AllGoods_Array addObject:model];
            }
            
            // 筛选数据到制定的位置
            [self FilterDataToEstablishedLocations];
        }
        else
        {
            NSString *errorString = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"error"]];
            
            [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:errorString SuperView:self.view];
        }
        
        [self.collectionView.mj_footer endRefreshing];
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 结束刷新
        [self.collectionView.mj_footer endRefreshing];
        
        // 提示数据请求失败
        [[HSQProgressHUDManger Manger] ShowProgressHUDPromptText:KErrorPlacherString SupView:self.view];
    }];
    
    
}

/**
 * @brief 筛选数据到制定的位置
 */
- (void)FilterDataToEstablishedLocations{
    
    if (self.categoryId.integerValue == -100) // 分类
    {
        [self.Class_source addObjectsFromArray:self.AllGoods_Array];
    }
    else
    {
        for (HSQGoodsDataListModel *model_01 in self.AllGoods_Array) {
            NSString *categoryId1 = [NSString stringWithFormat:@"%@",model_01.goodsCommon[@"categoryId1"]];
            if ([categoryId1 isEqualToString:self.categoryId]) {
                [self.Class_source addObject:model_01];
            }
        }
    }
    
    if (self.Current_String.integerValue == 0) // 默认筛选
    {
        [self.default_source addObjectsFromArray:self.Class_source];
        [self.dataSource addObjectsFromArray:self.default_source];
    }
    else if (self.Current_String.integerValue == 1) // 降价筛选
    {
        NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"goodsCommon.appPrice0" ascending:YES];
        
        NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
        
        NSMutableArray *paixu_array = [NSMutableArray arrayWithArray:self.Class_source];
        
        paixu_array = [[paixu_array sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];
        
        [self.dataSource addObjectsFromArray:paixu_array];
    }
    else if (self.Current_String.integerValue == 2) // 促销
    {
        for (NSInteger i = 0; i < self.Class_source.count; i ++) {
            HSQGoodsDataListModel *model_02 = self.Class_source[i];
            if ([model_02.goodsCommon[@"joinBigSale"] integerValue] == 1){
                [self.Sales_source addObject:model_02];
            }
        }
        [self.dataSource addObjectsFromArray:self.Sales_source];
    }
}


/**
 * @brief 底部的删除视图
 */
- (void)addBottomDeleteView{
    
    // 删除视图
    UIView *dete_View = [[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 50)];
    dete_View.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:dete_View];
    self.Dete_View = dete_View;
    
    // 删除的按钮
    UIButton *Dete_Button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    Dete_Button.frame = CGRectMake(KScreenWidth - 100, 0, 100, dete_View.mj_h);
    Dete_Button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [Dete_Button setTitle:@"删除" forState:(UIControlStateNormal)];
    [Dete_Button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [Dete_Button setBackgroundImage:[UIImage ImageWithColor:[UIColor redColor]] forState:(UIControlStateNormal)];
    [Dete_Button addTarget:self action:@selector(Dete_ButtonClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [dete_View addSubview:Dete_Button];
    self.Dete_Button = Dete_Button;
    
    // 选中的个数
    UILabel *Select_Label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, KScreenWidth - 110, dete_View.mj_h)];
    Select_Label.textColor = RGB(160, 150, 150);
    Select_Label.font = [UIFont systemFontOfSize:14.0];
    NSString *count = [NSString stringWithFormat:@"已选择%@种商品",@"0"];
    NSMutableAttributedString *attribe = [[NSMutableAttributedString alloc] initWithString:count];
    [attribe addAttribute:NSForegroundColorAttributeName value:RGB(238, 58, 68) range:NSMakeRange(3, 1)];
    Select_Label.attributedText = attribe;
    [dete_View addSubview:Select_Label];
    self.Select_Label = Select_Label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if (self.currentPage == self.totalPage.integerValue || self.totalPage.integerValue == 0)
    {
        self.collectionView.mj_footer.hidden = YES;
    }
    else
    {
        self.collectionView.mj_footer.hidden = NO;
    }
    
    self.noDataView.hidden = (self.dataSource.count != 0);
    
    return self.dataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(KScreenWidth, 140);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    HSQMyCollectionHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HSQMyCollectionHeaderView" forIndexPath:indexPath];

    headView.delegate = self;
    
    headView.model = self.dataSource[indexPath.section];
    
    headView.section = indexPath.section;
    
    return headView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(KScreenWidth, 120);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HSQGoodsCollectionListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HSQGoodsCollectionListCell" forIndexPath:indexPath];
    
    [cell.OrginPrice_label setHidden:YES];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    
}

/**
 * @brief 右下角三个点的按钮的点击事件
 */
- (void)ClickOnTheButtonInTheLowerRightCorner:(UIButton *)sender{
    
    HSQMyCollectionHeaderView *HeaderView = (HSQMyCollectionHeaderView *)sender.superview.superview;

    for (NSInteger i = 0; i < self.dataSource.count; i++) {
        
        HSQGoodsDataListModel *model = self.dataSource[i];
        
        model.IsOpen = (i == HeaderView.section ? @"1" : @"0");
    }
    
     [self.collectionView reloadData];
}

/**
 * @brief 立即分享的按钮点击事件
 */
- (void)ShareTheButtonImmediatelyByClickingTheEvent:(UIButton *)sender{
    
    HSQLog(@"====你点击了立即分享");
}

/**
 * @brief 加入购物车的按钮点击事件
 */
- (void)AddTheCartButtonClickEvent:(UIButton *)sender{
    
    HSQMyCollectionHeaderView *headView = (HSQMyCollectionHeaderView *)sender.superview.superview.superview;
    
    HSQGoodsDataListModel *model = self.dataSource[headView.section];
    
    self.commonId = model.commonId;
    
    // 请求商品详情的数据
    NSString *url = [NSString stringWithFormat:@"%@/group/%@",@"http://10.1.8.238/api",model.commonId];
    
    [[HSQProgressHUDManger Manger] ShowLoadingDataFromeServer:@"" ToView:self.view IsClearColor:YES];
    
    AFNetworkRequestTool *RequestTool = [AFNetworkRequestTool shareRequestTool];
    
    [RequestTool.manger GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [[HSQProgressHUDManger Manger] DismissProgressHUD];
        
        HSQLog(@"=拼团数据=%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 200)
        {
            HSQGoodsModelView *GuiGeAndCouperView = [HSQGoodsModelView initGoodsModelView];

            GuiGeAndCouperView.TypeString = @"100";
            
            GuiGeAndCouperView.Source = 200;

            GuiGeAndCouperView.dataDiction = responseObject[@"datas"];

            GuiGeAndCouperView.delegate = self;

            [GuiGeAndCouperView ShowGoodsModelAndPriceView];
        }
        else
        {
            NSString *errorString = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"error"]];
            
            [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:errorString SuperView:self.view];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:KErrorPlacherString SuperView:self.view];
        
    }];
    

}

-(void)hsqGoodsModelViewBottomBtnClickActionWithGoodsCount:(NSString *)Count Type:(NSString *)typeString goods_id:(NSString *)goodsId GoodsKunCun:(NSString *)goodsStorage goodsSpecString:(NSString *)goodsSpecString{
    
    HSQLog(@"==选好的商品个数==%@==%@==%@",Count,typeString,goodsId);
    
    if (typeString.integerValue == 100) // 将商品加入到购物车
    {
        [self AddItemsToTheShoppingCart:Count Goods_id:goodsId];
    }
    else // 开团按钮的点击
    {
        
    }
    
}

/**
 * @brief 将商品添加到购物车
 * @params BuyNumber 购买的数量
 */
- (void)AddItemsToTheShoppingCart:(NSString *)BuyNumber Goods_id:(NSString *)goodsId{
    
    // 购物车管理工具
    HSQShopCarManger *ShopCarManger = [HSQShopCarManger sharedShopCarManger];
    
    // 添加购物车数据
    NSDictionary *buyData_diction = @{@"buyNum":BuyNumber,@"goodsId":goodsId};
    NSMutableArray *goods_array = [NSMutableArray array];
    [goods_array addObject:buyData_diction];
    NSString *buydata = [ShopCarManger toJSONDataString:goods_array];
    
    // 要加入购物车的数据
    NSDictionary *cartData_diction = @{goodsId:@(BuyNumber.integerValue)};
    NSString *cartData = [ShopCarManger toJSONDataString:cartData_diction];
    
    // 购物车数据模型
    HSQShopCarGoodsListModel *ShopCarModel = [[HSQShopCarGoodsListModel alloc] init];
    ShopCarModel.goodsId = goodsId;
    ShopCarModel.buyData = buydata;
    ShopCarModel.cartData = cartData;
    ShopCarModel.commonId = self.commonId;
    ShopCarModel.buyNum = BuyNumber;
    
    // 1.判断用户购物车中是否有该商品
    BOOL isExit =  [ShopCarManger LoookUpGoodsIsExitWithGoods_id:goodsId];
    
    if (isExit == YES) // 该商品存在，更新本地的数据
    {
        [ShopCarManger updatePGoodsModel:ShopCarModel];
    }
    else // 不存在，将该商品添加到本地购物车中
    {
        [ShopCarManger addGoodsModel:ShopCarModel];
    }
    
    // 1.判断用户是否登录
    HSQAccount *accountTool = [HSQAccountTool account];
    
    [self WhenNotLoggedInAddTheItemToTheCartAndUploadItToTheServer:buydata cartData:@"" token:accountTool.token];   
    
}

/**
 * @brief 没有登录的时候，将商品添加到购物车，并上传至服务器
 */
- (void)WhenNotLoggedInAddTheItemToTheCartAndUploadItToTheServer:(NSString *)buyData cartData:(NSString *)cartData token:(NSString *)token{
    
    [[HSQProgressHUDManger Manger] ShowLoadingDataFromeServer:@"" ToView:self.view IsClearColor:YES];
    
    NSDictionary *params = @{@"buyData":buyData,@"clientType":KClientType,@"bundlingId":@"",@"token":token};
    
    HSQLog(@"==购物车参数==%@",params);
    
    AFNetworkRequestTool *RequestTool = [AFNetworkRequestTool shareRequestTool];
    
    [RequestTool.manger POST:UrlAdress(KaddGoodsToShopCarUrl) parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [[HSQProgressHUDManger Manger] DismissProgressHUD];
        
        HSQLog(@"=添加购物车数据==%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200)
        {
            [[HSQProgressHUDManger Manger] ShowProgressHUDPromptText:@"添加购物车成功" SupView:self.view];
        }
        else
        {
            NSString *errorString = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"error"]];
            
            [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:errorString SuperView:self.view];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:@"网络出问题啦！" SuperView:self.view];
    }];
}

/**
 * @brief 编辑按钮的点击
 */
- (void)EditNotif:(NSNotification *)notif{
    
    NSString *IsEditState = notif.userInfo[@"IsEditState"];
    
    self.IsEditState = IsEditState.integerValue;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.Dete_View.frame = ({
            
            CGRect Dete_View_Frame = self.Dete_View.frame;
            
            Dete_View_Frame.origin.y = (IsEditState.integerValue == 2 ? KScreenHeight - KSafeTopeHeight - KSafeBottomHeight - 50 : KScreenHeight);
            
            Dete_View_Frame;
            
        });
    }];
    
    self.collectionView.mj_header.hidden = self.collectionView.mj_footer.hidden = (IsEditState.integerValue == 2 ? YES : NO);
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0,  (IsEditState.integerValue == 2 ? 50 : 0), 0);
    
    for (HSQGoodsDataListModel *model in self.dataSource) {
        
        model.IsEditState = (IsEditState.integerValue == 2 ? @"2" : @"1");
        
        model.IsOpen = @"0";
    }
    
    [self.collectionView reloadData];
}

/**
 * @brief 编辑时选中按钮的点击
 */
- (void)SelectTheClickEventOfTheButtonWhenEditing:(UIButton *)sender{
    
    HSQMyCollectionHeaderView *headView = (HSQMyCollectionHeaderView *)sender.superview.superview;
    
    HSQGoodsDataListModel *model = self.dataSource[headView.section];
    
    if (model.IsSelectState.integerValue == 1) // 没有编辑
    {
        model.IsSelectState = @"2";
        
        [self.Select_Array addObject:model.commonId];
    }
    else
    {
        model.IsSelectState = @"1";
        
         [self.Select_Array removeObject:model.commonId];
    }
    
    [self.collectionView reloadData];
    
    NSString *count = [NSString stringWithFormat:@"%lu",(unsigned long)self.Select_Array.count];
    
    NSMutableAttributedString *attribe = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已选择%@种商品",count]];
    
    [attribe addAttribute:NSForegroundColorAttributeName value:RGB(238, 58, 68) range:NSMakeRange(3, count.length)];
    
    self.Select_Label.attributedText = attribe;
    
}

/**
 * @brief 删除按钮的点击
 */
- (void)Dete_ButtonClickAction:(UIButton *)sender{
    
    if (self.Select_Array.count ==0)
    {
        [[HSQProgressHUDManger Manger] ShowProgressHUDPromptText:@"请选择你要删除的商品" SupView:self.view];
    }
    else
    {
        NSString *Count = [NSString stringWithFormat:@"确认要删除这%lu种商品吗？",(unsigned long)self.Select_Array.count];
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:Count preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *Cancel_action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        
        UIAlertAction *delete_action = [UIAlertAction actionWithTitle:@"删除" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self DeteSelectGoodsFromeServer];
        }];
        
        [alertVC addAction:delete_action];
        
        [alertVC addAction:Cancel_action];
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}

/**
 * @brief 删除选中的商品
 */
- (void)DeteSelectGoodsFromeServer{
    
    [[HSQProgressHUDManger Manger] ShowLoadingDataFromeServer:@"" ToView:self.view IsClearColor:YES];
    
    NSString *commonIds = [self.Select_Array componentsJoinedByString:@","];
    
    NSDictionary *params = @{@"token":[HSQAccountTool account].token,@"commonIds":commonIds};
    
    AFNetworkRequestTool *requestTool = [AFNetworkRequestTool shareRequestTool];
    
    [requestTool.manger POST:UrlAdress(KBatchDeleteGoodsUrl) parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [[HSQProgressHUDManger Manger] DismissProgressHUD];
        
        HSQLog(@"==删除===%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 200)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DeteleGoodsSuccessFul" object:self];
            
            for (NSInteger i = 0; i < self.Select_Array.count; i++) {
                
                NSString *commid = self.Select_Array[i];
                
                for (NSInteger j = 0; j < self.dataSource.count; j++) {
                    
                    HSQGoodsDataListModel *model = self.dataSource[j];
                    
                    if ([commid isEqual:model.commonId]) {
                        
                        [self.dataSource removeObject:model];
                    }
                }
            }
            
            [self.collectionView reloadData];
        }
        else
        {
            NSString *errorString = [NSString stringWithFormat:@"%@",responseObject[@"datas"][@"error"]];
            
            [[HSQProgressHUDManger Manger] ShowDisplayFailedToLoadData:errorString SuperView:self.view];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [[HSQProgressHUDManger Manger] ShowProgressHUDPromptText:@"网络出问题啦！" SupView:self.view];
    }];
}

/**
 * @brief 点击进入商品详情
 */
- (void)JoinGoodsDetailClickAction:(UITapGestureRecognizer *)tapGR{
    
    HSQMyCollectionHeaderView *headerView = (HSQMyCollectionHeaderView *)tapGR.view.superview.superview;
    
    HSQGoodsDataListModel *model = self.dataSource[headerView.section];
    
    MVGoodsDetailHomeViewController *GoodsDetailVC = [[MVGoodsDetailHomeViewController alloc] init];
    
    GoodsDetailVC.commond_id = model.commonId;
    
    [self.navigationController pushViewController:GoodsDetailVC animated:YES];
}

/**
 * @brief 按钮背景视图的点击事件
 */
- (void)ButtonBgViewClickAction:(UITapGestureRecognizer *)tap{
    
    for (NSInteger i = 0; i < self.dataSource.count; i++) {
        
        HSQGoodsDataListModel *model = self.dataSource[i];
        
        model.IsOpen = @"0";
    }
    
    [self.collectionView reloadData];
}




-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
